// components/poster/poster.js
const app = getApp();
Component({
  options: {
      multipleSlots: true,
      addGlobalClass: true,
  },
  /**
   * 组件的属性列表
   */
  properties: {
    car:{
      type: Object,
      value:{}
    },
    dealerInfo:{
      type: Object,
      value:{}
    },
    needQrCode:{
      type: Boolean,
      value: true
    },
    soldOk:{
      type: Boolean,
      value: false
    },
    recommendReasons:{
      type: String,
      value:''
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    shareImage:'',
    paintPallette:'',
    showMore: false,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    showOrhiden(){
      let self = this;
      this.setData({
        'showMore': !self.data.showMore
      })
    },
    showShareModal(usercode){
      this.setData({
        'shareImage':''
      })
      let car = this.data.car;
      // 请求小程序二维码
      let userQrcode = 'data:image/png;base64,' + usercode;
      let dealerInfo = this.data.dealerInfo;
      app.showLoading('正在展开分享');
      let paintPallette = {
      width: '1312rpx',
      height: '2240rpx',
      background: '#fff',
      borderRadius: '24rpx',
      views:[
          {
          type: 'rect',
          css: {
              width: '1188rpx',
              height: '252rpx',
              color: '#F0915C',
              top: `66rpx`,
              left: `62rpx`,
              borderRadius:`252px`,
          }
          },
          {
          type: 'image',
          url: '/images/share/car.png',
          css: {
              width: `360rpx`,
              height: `360rpx`,
              top:`36rpx`,
              left: `100rpx`
          }
          },
          {
          type: 'text',
          text: '具足好车千万 助你实现梦想',
          css: {
              fontSize: '74rpx',
              top: `152rpx`,
              left: `208rpx`,
              color: `#FFFFFF`,
          }
          },
          {
          type: 'image',
          url: car.coverUrl,
          css: {
              width: `1188rpx`,
              height: `1188rpx`,
              top:`380rpx`,
              left: `62rpx`,
          }
          },
          {
          type: 'image',
          url: dealerInfo.logoUrl,
          css: {
              width: `294rpx`,
              height: `294rpx`,
              top: `1880rpx`,
              left: `62rpx`,
              borderRadius:`294rpx`,
          }
          },
          {
          type: 'text',
          text: dealerInfo.dealerName,
          css: {
              fontSize: '56rpx',
              top: `1940rpx`,
              left: `380rpx`,
              color: `#101010`,
          }
          },
          {
          type: 'text',
          text: '我在具足好车网',
          css: {
              fontSize: '56rpx',
              top: `2040rpx`,
              left: `380rpx`,
              color: `#BCBCBC`,
          }
          }
      ]
      };
      if(this.data.needQrCode){
        paintPallette.views.push(
          {
            type: 'image',
            url: userQrcode,
            css: {
              width: `294rpx`,
              height: `294rpx`,
              top: `1880rpx`,
              left: `956rpx`,
              borderRadius:`354rpx`,
            }
          }
        );
      }
      if(this.data.soldOk){
        paintPallette.views.push(
          {
            type: 'image',
            url: '/images/share/sold.png',
            css: {
              width: `1114rpx`,
              height: `1114rpx`,
              top: `800rpx`,
              left: `310rpx`
            }
          }
        );
      }
      let hindex = 0;
      let vehicleNameList = [];
      while (hindex < car.vehicleName.length) {
      vehicleNameList.push(car.vehicleName.slice(hindex,hindex+=22));
      }
      vehicleNameList.forEach((item,index) =>{
      paintPallette.views.push(
          {
          type: 'text',
          text: item,
          css: {
              width: `1188rpx`,
              fontSize: '64rpx',
              top: (1600+(index*80)) + 'rpx',
              left: `62rpx`,
              color: `#101010`,
              scalable: true,
          }
          }
      );
      })
      if(this.getTabBar()){
        this.getTabBar().hideTabbar();
      }
      this.setData({
        'paintPallette':paintPallette,
        'modalName': 'shareModal'
      });
    },
    onImgOK(e) {
      console.log('分享图生成完毕');
      this.imagePath = e.detail.path;
      wx.hideLoading();
      this.setData({
        shareImage: this.imagePath,
      });
    },
    hideModal(e) {
      if(this.getTabBar()){
        this.getTabBar().showTabbar();
      }
      this.setData({
        'modalName': null
      })
    },
    // 保存分享的图片
    saveShareImage() {
      let self = this;
      //图片保存到本地相册
      wx.saveImageToPhotosAlbum({
        filePath: self.data.shareImage,
        //授权成功，保存图片
        success: function (data) {
          app.success('已保存到您的相册');
        },
        //授权失败
        fail: function (err) {
          console.log(err);
          if (err.errMsg != 'saveImageToPhotosAlbum:fail cancel') { //重新授权弹框确认
            wx.showModal({
              title: '提示',
              content: '您好，请先授权再保存此图片。',
              showCancel: false,
              success(res) {
                if (res.confirm) { //重新授权弹框用户点击了确定
                  wx.openSetting({ //进入小程序授权设置页面
                    success(settingdata) {
                      console.log(settingdata)
                      if (settingdata.authSetting['scope.writePhotosAlbum']) { //用户打开了保存图片授权开关
                        wx.saveImageToPhotosAlbum({
                          filePath: self.data.shareImage,
                          success: function (data) {
                            app.success('已保存到您的相册');
                          },
                        })
                      } else { //用户未打开保存图片到相册的授权开关
                        wx.showModal({
                          title: '温馨提示',
                          content: '授权失败，请稍后重新获取',
                          showCancel: false,
                        })
                      }
                    }
                  })
                }
              }
            })
          }
        }
      })
    },
    copyRecommendReasons(){
        let recommendReasons = this.data.recommendReasons;
        wx.setClipboardData({
          data: recommendReasons,
          success: function (res) {
            wx.getClipboardData({
              success: function (res) {
                wx.showToast({
                  title: '已为您复制推荐理由',
                  icon:'none'
                })
              }
            })
          }
        })
    },
    doNothing(){},
  }
})
